﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ESRI.ArcGIS.Geodatabase;

namespace ArcMap_AddIn_Tools.Core.AOWrapper
{
    //字段处理相关类
    //包括字段检查等等
    class FieldWrapper
    {
        //检查字段为空的项
        //包括null和空字符串
        public static List<IFeature> checkIsNull(IFeatureClass pFeatureClass, String[] fieldName)
        {
            if (pFeatureClass == null) return null;
            List<IFeature> list = new List<IFeature>();//objectid,属性名
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                int index = pFeature.Fields.FindField(fieldName[0]);
                if (index != -1)
                {
                    //判断是否为空
                    if (string.IsNullOrEmpty(pFeature.get_Value(index).ToString()))
                    {
                        //String[] str = new String[] { pFeature.get_Value(0).ToString(), fieldName[1] };
                        list.Add(pFeature);
                    }
                }
                else { break; }
                pFeature = pFeatureCursor.NextFeature();
            }
            return list;
        }
       
        //检查字段是否唯一
        public static List<IFeature> checkIsUnique(IFeatureClass pFeatureClass, String[] fieldName)
        {
            if (pFeatureClass == null) return null;
            List<IFeature> list = new List<IFeature>();//objectid,属性名
            Dictionary<String, String> keyvalue = new Dictionary<string, string>();
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCursor.NextFeature();
            int index = pFeature.Fields.FindField(fieldName[0]);
            if (index != -1)
            {
                while (pFeature != null)
                {
                    if (keyvalue.ContainsKey(pFeature.get_Value(index).ToString()))
                    {
                        // String[] str = new String[] { pFeature.get_Value(0).ToString(), fieldName[1] };
                        list.Add(pFeature);
                    }
                    else
                    {
                        try
                        {
                            keyvalue.Add(pFeature.get_Value(index).ToString(), fieldName[0]);
                        }
                        catch (Exception ex)
                        { }
                    }
                    pFeature = pFeatureCursor.NextFeature();
                }
            }
            return list;
        }

        //检查字段长度是否符合定义
        public static List<IFeature> checkLength(IFeatureClass pFeatureClass, String[] fieldName, int length)
        {
            if (pFeatureClass == null) return null;
            List<IFeature> list = new List<IFeature>();
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, false);
            IFeature pFeature = pFeatureCursor.NextFeature();
            int index = pFeature.Fields.FindField(fieldName[0]);
            if (index != -1)
            {
                while (pFeature != null)
                {
                    String value = pFeature.get_Value(index).ToString();
                    if (value.Length != length)
                    {
                        String[] str = new String[] { pFeature.get_Value(0).ToString(), fieldName[1], length.ToString() };
                        list.Add(pFeature);
                    }
                    pFeature = pFeatureCursor.NextFeature();
                }
            }
            return list;
        }
    }
}
